package leetcode_双指针;

import java.util.Arrays;

/**
 * 移动零
 * 算法：双指针
 */
public class _283 {
    public void main(String[] args) {
        int[] nums = {0, 1, 0, 3, 12};
        new _283().moveZeroes(nums);
        System.out.println(Arrays.toString(nums));
    }

    public void moveZeroes_new(int[] nums) {
        int j = 0, n = nums.length;
        for (int i = 0; i < n; i++) if (nums[i] != 0) nums[j++] = nums[i];
        for (; j < n; j++) nums[j] = 0;
    }

    public void moveZeroes(int[] nums) {
        if (nums == null) {
            return;
        }
        int j = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                nums[j++] = nums[i];
            }
        }
        for (int i = j; i < nums.length; i++) {
            nums[i] = 0;
        }
    }

}
